<template>
  <div class="address">
    <app-header>
      <img slot="title" @click="prev" src="../assets/上一步.png" alt="">
      <p slot="city">我的地址</p>
    </app-header>
    <div class="container" ref="wrapper">
      <div>
        <div class="address__item" v-for="(item, index) in addresses" :key="item.id">
          <div class="item-left">
            <div class="item-left__top">
              <span>{{item.name}}</span>
              <span>{{item.sex|toGender}}</span>
              <span>{{item.phone}}</span>
            </div>
            <div class="item-left__down">
              <div class="item-tag">{{item.tag}}</div>
              <div class="item-address__details">{{item.address}}{{item.address_detail}}</div>
            </div>
          </div>
          <div class="item-right">
            <img src="../assets/编辑.png" alt="">
            <img @click="RemoveAddressById(item.id, index)" src="../assets/关闭.png" alt="">
          </div>
        </div>
      </div>
    </div>
    <div @click="AddAddress" class="address-footer">
      <div>+</div>
      <span>新增收货地址</span>
    </div>
  </div>
</template>

<script>
import AppHeader from './AppHeader.vue'
import { state } from '../store'
import { createDialog } from './simpleComponents/co-dialog'
import BScroll from 'better-scroll'

export default {
  name: 'Order',
  data () {
    return {
      addresses: []
    }
  },
  components: { AppHeader },
  methods: {
    prev () {
      this.$router.go(-1)
    },
    AddAddress () {
      this.$router.push({ name: 'AddAddress' })
    },
    RemoveDomByIndex (index) {
      this.addresses.splice(index, 1)
    },
    RemoveAddressById (addressId, index) {
      createDialog({
        title: '删除地址',
        message: '确认删除地址？'
      }).then(() => {
        this.$http.RemoveAddressById({
          user_id: state.user_id,
          address_id: addressId
        }).then(res => {
          console.log(res)
          if (res.data.status === 1) {
            this.RemoveDomByIndex(index)
          }
        })
      }).catch(() => {
        console.log('你后悔删除了')
      })
    }
  },
  created () {
    this.$http.Address({
      user_id: state.user_id
    }).then(res => {
      this.addresses = res.data
    }).catch(err => {
      console.log(err)
    })
  },
  filters: {
    toGender (value) {
      return value === 1 ? '先生' : '女士'
    },
    toType (value) {
    }
  },
  mounted () {
    this.scroller = new BScroll(this.$refs.wrapper, {
      scrollY: true,
      click: true
      // 下拉刷新
      /* pullDownRefresh: {
        threshold: 50
      },
      // 上拉加载
      pullUpLoad: {
        threshold: 50
      } */
    })
  }
}
</script>

<style scoped lang=scss>
  *{
    margin: 0px;
    padding: 0px;
  }
  .address{
    width: 100%;
    /* height: 177vw; */
    .container{
      height: 930px;
      overflow: hidden;
      .address__item{
        width: 100%;
        background-color: #fff;
        padding: 26px 30px 32px;
        box-sizing: border-box;
        display: flex;
        border: 1px solid #ddd;
        .item-left{
          width: 576px;
          .item-left__top{
            line-height: 46px;
            font-size: 30px;
            color: #666;
            span:nth-child(1){
              color: #333;
              font-weight: bold;
            }
          }
          .item-left__down{
            width: 576px;
            height: 65px;
            margin-top: 10px;
            display: flex;
            align-items: center;
            .item-tag{
              text-align: center;
              padding: 3px;
              border: 1px solid #3190e8;
              border-radius: 3px;
              color: #3190e8;
              font-size: 18px;
            }
            .item-address__details{
              width: 90%;
              font-size: 25px;
              color: #666;
              padding-left: 8px;
              box-sizing: border-box;
            }
          }
        }
        .item-right{
          width: 114px;
          display: flex;
          align-items: center;
          justify-content: center;
          img{
            width: 34px;
            padding-left: 10px;
          }
        }
      }
    }
    .address-footer{
      width: 100%;
      height: 102px;
      background-color: #fff;
      position: fixed;
      bottom: 0px;
      display: flex;
      align-items: center;
      justify-content: center;
      div{
        width: 41px;
        height: 41px;
        border-radius: 50%;
        border: 1px solid #4499ea;
        font-size: 50px;
        color: #4499ea;
        text-align: center;
        line-height: 35px;
      }
      span{
        font-size: 32px;
        color: #4499ea;
        padding-left: 15px;
        box-sizing: border-box;
      }
    }
  }
</style>
